package com.qf.web;

import com.alibaba.fastjson.JSON;
import com.qf.model.Massage;
import com.qf.model.Navig;
import com.qf.model.PageBean;
import com.qf.model.User;
import com.qf.service.NavigService;
import com.qf.service.impl.NavigServiceImpl;

import javax.jws.WebService;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@WebServlet("/Navig")
public class NavigServlet extends BascServlet{

    private NavigService navigService=new NavigServiceImpl();

    //查询导航栏信息
    public void findAll(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        User user=(User) req.getSession().getAttribute("user");
        //查询数据
        List<Navig> navigs = navigService.finNavig(user.getU_manager());
        //创建返回信息对象
        Massage<List<Navig>> m=new Massage<>(1,"查询成功",navigs);
        //转换成json格式
        String s = JSON.toJSONString(m);
        //响应给前端
        resp.getWriter().write(s);
    }

    //添加导航栏信息
    public void add(HttpServletRequest req, HttpServletResponse resp) throws Exception{
        String name = req.getParameter("name");
        int pid = Integer.parseInt(req.getParameter("pid"));
        String url = req.getParameter("url");

        Navig navig = new Navig(name, pid, url);
        NavigService navigService = new NavigServiceImpl();
        int i = navigService.addNavig(navig);
        Massage m = null;
        if (i > 0) {
            m = new Massage(1,  "添加成功");
        } else {
            m = new Massage(-1, "添加失败");
        }
        String s = JSON.toJSONString(m);
        resp.getWriter().write(s);
    }

    //根据id删除导航栏信息
    public void deleteByID(HttpServletRequest req, HttpServletResponse resp)throws Exception{
        int id=Integer.parseInt(req.getParameter("id"));
        NavigService navigService=new NavigServiceImpl();
        int num=navigService.deleteNavigByID(id);
        Massage m=new Massage(1,"删除成功");
        String s=JSON.toJSONString(m);
        resp.getWriter().write(s);
    }

    //根据id查询导航栏信息
    public void findByID(HttpServletRequest req, HttpServletResponse resp)throws Exception{
        int id=Integer.parseInt(req.getParameter("id"));
        NavigService navigService=new NavigServiceImpl();
        List<Navig> ns = navigService.findNavigByID(id);
        Massage<Navig> m=null;
        if(ns.size()>0){
            m=new Massage<>(1,"",ns.get(0));
        }else {
            m=new Massage<>(-1,"查询失败,请重试");
        }
        String s = JSON.toJSONString(m);
        resp.getWriter().write(s);
    }

    //修改
    public void updateByID(HttpServletRequest req, HttpServletResponse resp)throws Exception{
        int id=Integer.parseInt(req.getParameter("id"));
        String name=req.getParameter("name");
        int pid=Integer.parseInt(req.getParameter("pid"));
        String url=req.getParameter("url");

        Navig navig=new Navig(id,name,pid,url);
        NavigService navigService=new NavigServiceImpl();
        int i = navigService.updateNavige(navig);

        Massage m=null;
        if(i>0){
            m=new Massage(1,"");
        }else {
            m=new Massage(-1,"修改失败");
        }
        String s = JSON.toJSONString(m);
        resp.getWriter().write(s);
    }

    //查询带分页
    public void findAllByPage(HttpServletRequest req, HttpServletResponse resp)throws Exception{
        //获取前端传入的数据
        String a=req.getParameter("page");
        int page=Integer.parseInt(req.getParameter("page"));
        int pagesize=Integer.parseInt(req.getParameter("pagesize"));
        String name=req.getParameter("name");


        PageBean<Navig> pageBean=new PageBean<>();
        pageBean.setCurPage(page);
        pageBean.setPageSize(pagesize);

        NavigService navigService=new NavigServiceImpl();
        List<Navig> navigs = navigService.finNavigAll(pageBean,name);

        pageBean.setData(navigs);

        if(pageBean.getCount()%pageBean.getPageSize()==0){
            pageBean.setTotalPage(pageBean.getCount()/pageBean.getPageSize());
        }else {
            pageBean.setTotalPage(pageBean.getCount()/pageBean.getPageSize()+1);
        }

        //创建返回信息对象
        Massage<PageBean<Navig>> m=new Massage<>(1,"查询成功",pageBean);
        //转换成json格式
        String s = JSON.toJSONString(m);
        //响应给前端
        resp.getWriter().write(s);
    }
}
